<template>
  <div class="question-list-container">
    <div class="question-list-header">
      <h3>题目列表</h3>
      <div class="search-box">
        <input type="text" v-model="searchQuery" placeholder="搜索题目..." />
        <i class="search-icon">🔍</i>
      </div>
    </div>

    <transition-group name="list" tag="ul" class="question-items">
      <li v-for="question in filteredQuestions" :key="question.id" class="question-item"
      @click="selectQuestion(question.id)" :class="{ 'selected': selectedQuestion?.id === question.id }">
        <div class="question-number">{{ question.id }}</div>
        <div class="question-title">{{ question.title }}</div>
        <div class="question-actions">
          <button class="action-btn" >查看</button>
        </div>
      </li>
    </transition-group>

    <div v-if="filteredQuestions.length === 0" class="no-results">
      没有找到匹配的题目
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { QuestionBankApi } from '@/api/QuestionBank/QuestionBank'
import {useQuestionBanTopicStore} from '@/store/modules/questionBank/questionbankdata'

interface Question {
  id: number
  title: string
}

export default defineComponent({
  name: 'QuestionList',
  setup() {
    // 路由
    const router = useRouter()
    const questionBankDataStore = useQuestionBanTopicStore();

    // 测试数据
    const questions = ref<Question[]>([
      { id: 1003, title: 'Java 内存模型（JMM）与多线程中的内存可见性问题' },
      { id: 1004, title: '录入成绩' },
      { id: 1033, title: '动态内存分配与内存泄漏' },
      { id: 1034, title: 'Java 继承与多态' },
      { id: 1035, title: '指针与数组的关系' },
      { id: 1036, title: '预处理器与宏' },
      { id: 1037, title: '函数指针与回调' },
      { id: 1038, title: '文件操作与错误处理' }
    ])

    const searchQuery = ref('')
    const selectedQuestion = ref<Question | null>(null)

    const filteredQuestions = computed(() => {
      if (!searchQuery.value) return questions.value
      const query = searchQuery.value.toLowerCase()
      return questions.value.filter(q =>
        q.title.toLowerCase().includes(query) ||
        q.id.toString().includes(query)
      )
    })

    const selectQuestion = (id: number) => {
      console.log(id);
      
      QuestionBankApi.getQuestionBankListById({
        exercisesId: id
      }).then(res => {
        console.log(res);
        if (res.data != null) {
        questionBankDataStore.ChangeQuestion = res.data;
        questionBankDataStore.handChangeQuestion(res.data);
        router.push(`/questionBank/questionBankDetails/id:${id}`);
        }
      });
    }

    return {
      questions,
      searchQuery,
      filteredQuestions,
      selectedQuestion,
      selectQuestion
    }
  }
})
</script>

<style scoped>
.question-list-container {
  /* max-width: 800px; */
  height: 100%;
  overflow-y: auto;
  margin: 0 auto;
  padding: 24px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background-color: rgb(251, 251, 250);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.question-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

h2 {
  color: #2c3e50;
  margin: 0;
  font-size: 1.8rem;
  font-weight: 600;
}

.search-box {
  position: relative;
  width: 250px;
}

.search-box input {
  width: 100%;
  padding: 10px 16px 10px 40px;
  border: none;
  border-radius: 20px;
  background-color: rgba(52, 152, 219, 0.1);
  font-size: 0.9rem;
  color: #34495e;
  transition: all 0.3s ease;
  border: 1px solid rgba(52, 152, 219, 0.2);
}

.search-box input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.3);
  background-color: rgba(52, 152, 219, 0.05);
  border-color: rgb(52, 152, 219);
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: rgb(52, 152, 219);
  font-size: 0.9rem;
}

.question-items {
  list-style: none;
  padding: 0;
  margin: 0;
}

.question-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-radius: 6px;
  margin-bottom: 10px;
  background-color: #fff;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.question-item:hover {
  background-color: rgba(251, 251, 250, 0.8);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}

.question-item.selected {
  background-color: rgba(227, 242, 253, 0.5);
  border-left: 4px solid #3498db;
}

.question-number {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 36px;
  height: 36px;
  background-color: #3498db;
  color: white;
  border-radius: 50%;
  font-weight: bold;
  margin-right: 16px;
  flex-shrink: 0;
}

.question-title {
  flex-grow: 1;
  font-size: 1rem;
  color: #2c3e50;
}

.question-actions {
  opacity: 0;
  transition: opacity 0.2s ease;
}

.question-item:hover .question-actions {
  opacity: 1;
}

.action-btn {
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.action-btn:hover {
  background-color: #2980b9;
}

.no-results {
  text-align: center;
  padding: 24px;
  color: #7f8c8d;
  font-style: italic;
}

/* 列表过渡动画 */
.list-enter-active,
.list-leave-active {
  transition: all 0.5s ease;
}

.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

.list-move {
  transition: transform 0.5s ease;
}
</style>